package com.financialDog.javaTool.controller.fileManage;


import com.financialDog.javaTool.controller.reportTools.ReportMergeToolController;
import com.financialDog.javaTool.services.fileManage.ExportManageService;
import com.financialDog.javaTool.utils.ExportUtil;
import com.financialDog.javaTool.view.fileManager.ExportManageView;
import com.xwintop.xcore.util.javafx.FileChooserUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class ExportManageController extends ExportManageView {

    public static final String FXML = "/com/financialDog/javaTool/fxmlView/reportTools/ExportFile.fxml";

    private ExportManageService exportManageService = new ExportManageService(this);

    private ReportMergeToolController reportMergeToolController;


    public static FXMLLoader getFXMLLoader() {
        return new FXMLLoader(ReportMergeToolController.class.getResource(FXML));
    }

    private String[] filtrationTypeChoiceBoxStrings = new String[] { "所有","资产负债表","现金流量表", "利润表"};

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initView();
        initEvent();
        initService();
    }

    private void initView() {
        filtrationTypeChoiceBox.getItems().addAll(filtrationTypeChoiceBoxStrings);
    }

    private void initEvent() {

    }

    private void initService() {
        //pluginManageService.getPluginList();
    }

    public void setReportMergeToolController(ReportMergeToolController reportMergeToolController){
        this.reportMergeToolController = reportMergeToolController;

        //初始化数据
	    initData();
    }

    @FXML
    private void chooseDir(ActionEvent event) {
        File file = FileChooserUtil.chooseDirectory();
        if (file != null) {
            projectPathTextField.setText(file.getPath());
        }
    }

    @FXML
    private void startExportExcel(ActionEvent event) {
        String filePath = projectPathTextField.getText();
        ReportMergeToolController reportMergeToolController = this.reportMergeToolController;
        String fileName = fileNameTextField.getText();
        String result = StringUtils.EMPTY;
        try {
            result = ExportUtil.exportReportToExcel(filePath, fileName,
                    reportMergeToolController.getTableDataViewMain(),
                    reportMergeToolController.getTableProfitView(),
                    reportMergeToolController.getTableMoneyView(),
                    reportMergeToolController.getTableAccountView());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(result)){
            TooltipUtil.showToast("导出成功");
        }else{
            TooltipUtil.showToast("导出失败:" + result);
        }
    }

    private void initData(){
    }

}
